// Definition for a binary tree node.
function TreeNode(val, left, right) {
  this.val = (val === undefined ? 0 : val)
  this.left = (left === undefined ? null : left)
  this.right = (right === undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function (root) {
  if (!root) return [];
  if (root && !root.left && !root.right) return [root.val];
  let result = [],
    stack = [];
  while (root != null || stack.length) {
    // 装载如左侧
    while (root != null) {
      stack.push(root);
      root = root.left;
    }
    // 清空栈
    if (stack.length) {
      root = stack.pop();
      result.push(root.val);
      root = root.right;
    }
  }
  return result;
};
